package com.controller;

import java.util.List;
import java.util.Map;











import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.LogDao;
import com.dao.UserDao;
import com.util.MybatisSessionFactory;

@Controller
public class UserController {
	
	@RequestMapping(value = "register.action", method = RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			@RequestParam(value = "password2", defaultValue = "") String password2) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao dao = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		Map<String, Object> result = dao.query(account);
		if (!password.equals(password2)) {
			return "NO";
		}
		if ("1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		dao.add(account, password, account, "2012-06-03");
		Map<String, Object> result1 = dao.queryid();

		dao1.addLog(result1.get("id").toString(), "0", "注册");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "/moneyIn.action", method = RequestMethod.POST)
	@ResponseBody
    public String moneyIn(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao dao = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		dao.addmoneyIn(userId, place, kind, money_in, remark, date_time);
		Map<String, Object> result1 = dao.queryid();
		dao1.addLog(userId, result1.get("id").toString(), "收入");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "/moneyOut.action", method = RequestMethod.POST)
	@ResponseBody
	public String moneyOut(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao dao = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);

		dao.addmoneyOut(userId, place, kind, money_out, remark, date_time);
		Map<String, Object> result1 = dao.queryid();
		dao1.addLog(userId, result1.get("id").toString(), "支出");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		//System.out.println(password);
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao c = session.getMapper(UserDao.class);
		LogDao dao1 = session.getMapper(LogDao.class);
		Map<String, Object> result = c.queryall(account, password);

		if (!"1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		response.addCookie(new Cookie("user", result.get("id").toString()));
		dao1.addLog(result.get("id").toString(), "0", "登录");
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "queryuser.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryuser(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		if ("".equals(userId)) {
			try {
				response.sendRedirect("login.html");
				return null;
			} catch (Exception e) {
				System.out.println("[error] queryUser.action:redirect\r\n");
				return null;
			}
		}
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.queryuser(userId);
		return map;
	}

	@RequestMapping(value = "query_money.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query_money(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.query_money(userId);
		return map;
	}

	@RequestMapping(value = "query_money_yue.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query_money_yue(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		Map<String, Object> map = d.query_money_yue(userId);
		return map;
	}

	@RequestMapping(value = "query_money_kindin.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_money_kindin(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.query_money_kindin(userId);
		System.out.println(map);
		return map;
	}

	@RequestMapping(value = "query_money_kindout.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_money_kindout(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.query_money_kindout(userId);
		System.out.println(map);
		return map;
	}
	
	@RequestMapping(value = "queryJQI.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> queryJQI(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.queryJQI(userId);
		return map;
	}

	@RequestMapping(value = "queryJQO.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> queryJQO(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.queryJQO(userId);
		return map;
	}

	@RequestMapping(value = "query_result.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Map> query_result(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map> map = d.query_result(userId, date_time, place, kind,
				money_in, money_out, remark);
		session.commit();
		return map;
	}

	@RequestMapping(value = "cleanCookie.action", method = RequestMethod.POST)
	@ResponseBody
	public String cleanCookie(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		LogDao dao1 = session.getMapper(LogDao.class);
		dao1.addLog(userId, "0", "退出");
		response.addCookie(new Cookie("user", null));
		session.commit();
		return "OK";
	}

	@RequestMapping(value = "queryOne.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> queryOne(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") String id) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		List<Map<String, Object>> map = d.queryOne(userId, id);
		session.commit();
		return map;
	}

	@RequestMapping(value = "delete.action", method = RequestMethod.POST)
	@ResponseBody
	public String delete(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") String id) {
		SqlSession session = MybatisSessionFactory.getSqlSession();
		UserDao d = session.getMapper(UserDao.class);
		System.out.println(id);
		d.del(userId, id);
		LogDao dao1 = session.getMapper(LogDao.class);
		dao1.addLog(userId, id, "删除");
		session.commit();
		return "OK";
	}
	// public static void main(String[] args) {
	// SqlSession session=MybatisSessionFactory.getSqlSession();
	// UserDao d=session.getMapper(UserDao.class);
	// Map<String,Object> map=d.queryuser();
	// Map<String,Object> map=d.query_moneyin("2");
	// }
}


